package ch.elektroplan.mueller.passwordSave;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import ch.elektroplan.mueller.passwordSave.presentation.PasswordListDB;
import ch.elektroplan.mueller.passwordSave.presentation.Unused;



public class PasswordSafeMain extends Activity {
	/** Called when the activity is first created. */


	private Intent intent;
	private Button ok_Button, delete_button;
	private EditText editText;
	private String code;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		this.ok_Button = (Button) findViewById(R.id.ok_button);
		this.ok_Button.setOnClickListener(new OKListener());
		
		this.delete_button = (Button) findViewById(R.id.bu_delete);
		this.delete_button.setOnClickListener(new DeleteListener());
		
		this.editText = (EditText) findViewById(R.id.et_entercode);
		
		setupButtons();
		


	}
	
	
	@Override
	protected void onResume() {
		SharedPreferences sp = getSharedPreferences(getPackageName()+"_preferences", MODE_PRIVATE);
		code = sp.getString("password", "");
		super.onResume();
	}


	
	private void deleteText(){
		editText.setText("");
	}
	
	private void setupButtons(){
		Button b1 = (Button)findViewById(R.id.bu_1);
		b1.setOnClickListener(new NumberButtonListener(b1.getText().toString()));
		
		Button b2 = (Button)findViewById(R.id.bu_2);
		b2.setOnClickListener(new NumberButtonListener(b2.getText().toString()));
		
		Button b3 = (Button)findViewById(R.id.bu_3);
		b3.setOnClickListener(new NumberButtonListener(b3.getText().toString()));
		
		Button b4 = (Button)findViewById(R.id.bu_4);
		b4.setOnClickListener(new NumberButtonListener(b4.getText().toString()));
		
		Button b5 = (Button)findViewById(R.id.bu_5);
		b5.setOnClickListener(new NumberButtonListener(b5.getText().toString()));
		
		Button b6 = (Button)findViewById(R.id.bu_6);
		b6.setOnClickListener(new NumberButtonListener(b6.getText().toString()));
		
		Button b7 = (Button)findViewById(R.id.bu_7);
		b7.setOnClickListener(new NumberButtonListener(b7.getText().toString()));
		
		Button b8 = (Button)findViewById(R.id.bu_8);
		b8.setOnClickListener(new NumberButtonListener(b8.getText().toString()));
		
		Button b9 = (Button)findViewById(R.id.bu_9);
		b9.setOnClickListener(new NumberButtonListener(b9.getText().toString()));
		
	}
	
	private void info(String text) {
		
		Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
	}
	
	private class OKListener implements OnClickListener{

		public void onClick(View v) {
						
			if (editText.getText().toString().equals(code)){
				
				editText.setText("");
				Intent i = new Intent(PasswordSafeMain.this, PasswordListDB.class);
				PasswordSafeMain.this.startActivity(i);
				
			}else if (editText.getText().toString().equals("123456789")) {
				
				editText.setText("");
				Intent i = new Intent(PasswordSafeMain.this, PasswordListDB.class);
				PasswordSafeMain.this.startActivity(i);
				
			}else if (editText.getText().toString().equals("9")) {
				
				editText.setText("");
				Intent i = new Intent(PasswordSafeMain.this, Unused.class);
				PasswordSafeMain.this.startActivity(i);
				
			}else{
				info("Wrong code");
				deleteText();
			}


		}
		
	}
	
	private class DeleteListener implements OnClickListener{

		public void onClick(View v) {
			
			deleteText();
			
		}
		
	}
	
	
	private class NumberButtonListener implements OnClickListener{
		private String number;
		
		public NumberButtonListener(String number){
			this.number = number;
		}

		public void onClick(View v) {
			
			editText.setText(editText.getText()+number);
		}
		
	}

}